//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.ObjectListDataProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.model.S3Bucket;

/**
 *
 * @author Sean ODell
 */
public class S3StorageBucketDataProvider extends ObjectListDataProvider {

    private ArrayList s3BucketList = new ArrayList();
    private S3Service s3Service;

    public S3StorageBucketDataProvider() {
        setList(s3BucketList);
        setObjectType(S3StorageBucket.class);
    }

    public void load(Properties awsProps, S3StorageConnection s3StorageConnection) {

        clearObjectList();

        if (awsProps == null) {
            return;
        }

        if (awsProps.getProperty("aws.accessKey").equals("")) {
            this.s3Service = null;
            return;
        }

        try {
            s3Service = s3StorageConnection.getS3Service();
            S3Bucket[] buckets = s3Service.listAllBuckets();

            for (int b = 0; b < buckets.length; b++) {
                S3StorageBucket s3StorageBucket = new S3StorageBucket();
                s3StorageBucket.setname(buckets[b].getName());
                s3StorageBucket.setcreationDate(buckets[b].getCreationDate());
                s3StorageBucket.setlocation(buckets[b].getLocation());
                getList().add(s3StorageBucket);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public S3StorageBucket getS3StorageBucket(RowKey rowKey) {
        S3StorageBucket s3StorageBucket = (S3StorageBucket) getObject(rowKey);
        return s3StorageBucket;
    }

    public S3StorageBucket getS3StorageBucket(String bname) {
        for (int i = 0; i < s3BucketList.size(); i++) {
            S3StorageBucket s3StorageBucket = (S3StorageBucket) s3BucketList.get(i);
            if (s3StorageBucket.getname().equals(bname)) {
                return s3StorageBucket;
            }
        }
        return null;
    }

    public ArrayList getS3BucketList() {
        return this.s3BucketList;
    }

    public String addS3StorageBuckets(List<String> bnames) {
        String statusMsg = new String();

        if (s3Service == null) {
            statusMsg = statusMsg + "Please update user settings first.";
            return statusMsg;
        }

        for (String bname : bnames) {
            try {
                s3Service.createBucket(bname);
                statusMsg = statusMsg + "Bucket added: " + bname + "\n";
            } catch (S3ServiceException e) {
                e.printStackTrace();
                if (e.getMessage() != null) {
                    statusMsg = statusMsg + "Error: " + e.getMessage() + " - bucket name: " + bname + "\n";
                }
            }
        }
        return statusMsg;
    }

    public String deleteS3StorageBuckets(List<String> bnames) {
        String statusMsg = new String();

        if (s3Service == null) {
            statusMsg = statusMsg + "Please update user settings first.";
            return statusMsg;
        }

        for (String bname : bnames) {
            try {
                S3Bucket bucket = s3Service.getBucket(bname);
                s3Service.deleteBucket(bucket);
                statusMsg = statusMsg + "Bucket deleted: " + bname + "\n";
            } catch (S3ServiceException e) {
                e.printStackTrace();
                if (e.getMessage() != null) {
                    statusMsg = statusMsg + "Error: " + e.getMessage() + " - bucket name: " + bname + "\n";
                }
            }
        }
        return statusMsg;
    }
}
